package com.gamefx.engine1.managers.soundClasses
{
	import com.gamefx.engine1.managers.SoundManagerImpl;
	
	import flash.events.IOErrorEvent;
	import flash.media.Sound;
	import flash.net.URLRequest;

	public class SoundBox
	{
		public var url:String;
		public var sound:Sound;
		
		public function SoundBox(url:String):void
		{
			this.url = url;
			sound = new Sound();
			sound.addEventListener(IOErrorEvent.IO_ERROR, sound_ioErrorHandler);
			sound.load(new URLRequest(url));
		}
		
		private var _playingCount:int;
		
		public function get playingCount():int
		{
			return _playingCount;
		}
		
		public function set playingCount(value:int):void
		{
			_playingCount = value;
			if (_playingCount < 1)
			{
				SoundManagerImpl.getInstance().destory(url);
			}
		}
		
		public function destory():void
		{
			try
			{
				sound.close();
				sound = null;
			}
			catch (e:*)
			{
			}
		}
		
		private function sound_ioErrorHandler(event:IOErrorEvent):void
		{
			trace(event.text);
			SoundManagerImpl.getInstance().destory(url, true);
		}
	}
}